#!/usr/bin/env python

from os.path import join

import random

from kapybara.language import *
from kapybara.database import FileDatabase

def pick_option(options, language=None):
    for i, o in options.iteritems():
        print i, ':', o.name(language)
    return random.sample(options.values(), 1)[0]

def test():
    # Connect to database
    db = FileDatabase(join('db', 'example.kap'))

    # Pick a language
    all_languages = db.languages()
    language = pick_option(all_languages)
    print('Language: {0}'.format(language))

    # Pick an expression class to work with
    e_classes = language.expression_classes()
    e_class = pick_option(e_classes, language)
    print('ExpressionClass: {0}'.format(e_class.name(language)))

    # Pick a random expression from the language
    expressions = language.expressions(e_class)
    expression = pick_option(expressions, language)
    print('Expression: {0}'.format(expression.name(language)))

    # Pick all functions from the language
    functions = language.functions(expression).values()

    for f in functions:
        print('{0}({1}) = {2}'.format(f.name(language),
            expression.name(language), f(expression, language)))

if __name__=='__main__':
    test()
